package com.sinothk.fc.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.fc.employee.entity.StoreEmployeeEntity;
import com.sinothk.fc.employee.entity.StoreEmployeeVO;
import com.sinothk.fc.employee.mapper.StoreEmployeeMapper;
import com.sinothk.fc.employee.service.IStoreEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 门店员工表
 * @Author: Liangyt
 * @Date: 2022-09-04
 * @Version: V1.0
 */
@Slf4j
@Service
public class StoreEmployeeServiceImpl extends ServiceImpl<StoreEmployeeMapper, StoreEmployeeEntity> implements IStoreEmployeeService {
    @Resource
    private StoreEmployeeMapper storeEmployeeMapper;

    @Override
    public IPage<StoreEmployeeVO> pageForCurrStore(Page<StoreEmployeeVO> page) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return storeEmployeeMapper.employeePageByStoreId(page, loginUser.getStoreId());
    }

//    @Override
//    public void addStoreEmployee(String storeId, String phone) throws Exception {
//        addStoreEmployee(storeId, phone, "", "store_member");
//    }

//    @Override
//    public void addStoreEmployee(String storeId, String phone, String role) throws Exception {
//        LambdaQueryWrapper<StoreEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(StoreEmployeeEntity::getStoreId, storeId)
//                .eq(StoreEmployeeEntity::getUserPhone, phone);
//        List<StoreEmployeeEntity> list = storeEmployeeMapper.selectList(wrapper);
//
//        if (list == null || list.size() == 0) {
//            StoreEmployeeEntity storeEmployeeEntity = new StoreEmployeeEntity();
//            storeEmployeeEntity.setStoreId(storeId);
//            storeEmployeeEntity.setUserPhone(phone);
//            if (StringUtils.isNotBlank(role)) {
//                storeEmployeeEntity.setWorkerNo(10000);
//                storeEmployeeEntity.setWorkerPost("店主");
//                storeEmployeeEntity.setRemark("门店店主");
//            }
//            storeEmployeeEntity.setWorkerRole(role);
//            storeEmployeeEntity.setEnterDate(new Date());
//            storeEmployeeMapper.insert(storeEmployeeEntity);
//        } else if (list.size() == 1) {
//            throw new Exception("此用户已在门店的员工中");
//        } else {
//            throw new Exception("此用户在门店员工中存在" + list.size() + "人");
//        }
//
//        addStoreEmployee(storeId, String userId, String phone, String role)
//    }

    @Override
    public void addStoreEmployee(String storeId, String userId, String phone, String role) throws Exception {
        LambdaQueryWrapper<StoreEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreEmployeeEntity::getStoreId, storeId)
                .eq(StoreEmployeeEntity::getUserPhone, phone);
        List<StoreEmployeeEntity> list = storeEmployeeMapper.selectList(wrapper);

        if (list == null || list.size() == 0) {
            StoreEmployeeEntity storeEmployeeEntity = new StoreEmployeeEntity();
            storeEmployeeEntity.setStoreId(storeId);
            storeEmployeeEntity.setUserId(userId);
            storeEmployeeEntity.setUserPhone(phone);
            if (StringUtils.isNotBlank(role)) {
                storeEmployeeEntity.setWorkerNo(10000);
                storeEmployeeEntity.setWorkerPost("店主");
                storeEmployeeEntity.setRemark("门店店主");
            }
            storeEmployeeEntity.setWorkerRole(role);
            storeEmployeeEntity.setEnterDate(new Date());
            storeEmployeeMapper.insert(storeEmployeeEntity);
        } else if (list.size() == 1) {
            throw new Exception("此用户已在门店的员工中");
        } else {
            throw new Exception("此用户在门店员工中存在" + list.size() + "人");
        }
    }

    @Override
    public void unbinding(String storeId, String employeePhone) {
        LambdaQueryWrapper<StoreEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreEmployeeEntity::getStoreId, storeId)
                .eq(StoreEmployeeEntity::getUserPhone, employeePhone);
        this.remove(wrapper);
    }

    @Override
    public StoreEmployeeEntity binding(String storeId, String employeePhone) throws Exception {
        LambdaQueryWrapper<StoreEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreEmployeeEntity::getStoreId, storeId)
                .eq(StoreEmployeeEntity::getUserPhone, employeePhone);
        StoreEmployeeEntity storeEmployeeEntityDB = this.getOne(wrapper);
        if (storeEmployeeEntityDB != null) throw new Exception("此人员已在门店员工中");

        StoreEmployeeEntity storeEmployeeEntity = new StoreEmployeeEntity();
        storeEmployeeEntity.setStoreId(storeId);
        storeEmployeeEntity.setUserPhone(employeePhone);
        storeEmployeeEntity.setWorkerRole("store_member");
        this.save(storeEmployeeEntity);

        return storeEmployeeEntity;
    }

    @Override
    public StoreEmployeeEntity getUserPostInfoInStore(String storeId, String userPhone) {
        LambdaQueryWrapper<StoreEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreEmployeeEntity::getStoreId, storeId)
                .eq(StoreEmployeeEntity::getUserPhone, userPhone);
        return this.getOne(wrapper);
    }

    @Override
    public void updateByStoreIdAndPhone(StoreEmployeeEntity storeEmployee) throws Exception {
        if (StringUtils.isBlank(storeEmployee.getStoreId())
                || StringUtils.isBlank(storeEmployee.getStoreId())) {
            throw new Exception("门店ID和手机号不能为空");
        }
        LambdaUpdateWrapper<StoreEmployeeEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StoreEmployeeEntity::getStoreId, storeEmployee.getStoreId())
                .eq(StoreEmployeeEntity::getUserPhone, storeEmployee.getUserPhone());
        this.update(storeEmployee, wrapper);
    }
}
